package morenoapp.components.workplace;

import java.awt.BorderLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import morenoapp.MorenoApp;
import morenoapp.util.I18N;

/**
 *
 * @author dmitry.mamonov
 */
public class TabbedWorkplace extends JPanel implements WorkplaceNavigator {
    private Icon helpIcon;
    private final Workplace workplace;
    private JTabbedPane tabs;
    ComponentListener tabbedComponentListener = new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            if (e.getComponent().
                    getWidth() < 1080) {
                applyTabsTitles(true);
            } else {
                applyTabsTitles(false);
            }
        }
    };

    /**
     * Creates new form MainTabsJPanel
     * @param _workplace
     */
    public TabbedWorkplace(Workplace _workplace) {
        super();
        this.workplace = _workplace;
        helpIcon = new ImageIcon(MorenoApp.class.getResource("resources/info-16.png")); //NOI18N
        this.setLayout(new BorderLayout());
        tabs = new JTabbedPane();
        tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        this.add(tabs, BorderLayout.CENTER);

        for (WorkplaceView view : WorkplaceView.values()) {
            tabs.addTab(
                    String.valueOf(view.ordinal()),
                    helpIcon,
                    new JPanel(),
                    view.getCaption());
        }
        tabs.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                workplace.setCurrentView(WorkplaceView.values()[tabs.getSelectedIndex()]);
            }
        });
        applyTabsTitles(false);
    }

    private void applyTabsTitles(boolean shortNames) {
        final String tailSpace = "   "; //NOI18N
        String keySuffix = shortNames == true ? ".short" : ".full"; //NOI18N
        for(WorkplaceView view:WorkplaceView.values()){
            tabs.setTitleAt(view.ordinal(), I18N.get("tab."+view.name() + keySuffix) + tailSpace);
        }
    }

    void init(Workplace aThis) {
        for(WorkplaceView view:WorkplaceView.values()){
            tabs.setComponentAt(view.ordinal(),  workplace.getView(view));
        }
        tabs.setSelectedIndex(workplace.getCurrentView().ordinal());
    }

    void cleanup() {
        //cleanup
        for (int tab = tabs.getTabCount() - 1; tab >= 0; tab--) {
            tabs.setComponentAt(tab, new JPanel());
        }
    }

    @Override
    public void activateView(WorkplaceView view) {
        workplace.setCurrentView(view);
        tabs.setSelectedIndex(view.ordinal());
    }

    @Override
    public boolean activateNextView() {
        return workplace.activateNextView();
    }

    @Override
    public boolean activatePrevView() {
        return workplace.activatePrevView();
    }

    @Override
    public WorkplaceView getCurrentView() {
        return workplace.getCurrentView();
    }

    @Override
    public JComponent getCurrentViewComponent() {
        return workplace.getCurrentViewComponent();
    }

    @Override
    public void addNavigatorListener(WorkspaceNavigatorListener lst) {
        workplace.addNavigatorListener(lst);
    }

    @Override
    public void removeNavigatoListener(WorkspaceNavigatorListener lst) {
        workplace.removeNavigatoListener(lst);
    }
}
